<?php
App::uses('AppController', 'Controller');
/**
 * Previews Controller
 *
 * @property Preview $Preview
 */
class PreviewsController extends AppController {

	public $uses = array('Preview', 'Upload');

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Preview->recursive = 0;
		$this->set('previews', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Preview->exists($id)) {
			throw new NotFoundException(__('Invalid preview'));
		}
		$options = array('conditions' => array('Preview.' . $this->Preview->primaryKey => $id));
		$this->set('preview', $this->Preview->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Preview->create();
			$this->Upload->create();
			if ($this->uploadFile() && $this->Upload->save($this->data['Upload'])) 
			{	
			
				
			
				if ($this->Preview->save($this->data['Preview'])) {
					$this->Session->setFlash(__('The preview has been saved'));
					$this->redirect(array('action' => 'index'));
				} else {
					$this->Session->setFlash(__('The preview could not be saved. Please, try again.'));
				}
			}
		}
		$stages = $this->Preview->Stage->find('list');
		$uploads = $this->Preview->Upload->find('list');
		$this->set(compact('stages', 'uploads'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Preview->exists($id)) {
			throw new NotFoundException(__('Invalid preview'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Preview->save($this->request->data)) {
				if($this->editUploadFile($this->request->data['Preview']['upload_id']))	
				   $this->Upload->save($this->request->data['Upload']);					
				$this->Session->setFlash(__('The preview has been saved'));
				$this->redirect(array('action' => 'index'));
				
			} else {
				$this->Session->setFlash(__('The preview could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Preview.' . $this->Preview->primaryKey => $id));
			$this->request->data = $this->Preview->find('first', $options);
		}
		$stages = $this->Preview->Stage->find('list');
		$uploads = $this->Preview->Upload->find('list');
		$this->set(compact('stages', 'uploads'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$params = explode('|',$id);
		$this->Preview->id = $params[0];
		if (!$this->Preview->exists()) {
			throw new NotFoundException(__('Invalid preview'));
		}
		$this->request->onlyAllow('post', 'delete');
		$this->Upload->id = $params[1];
		
		if ($this->Preview->delete() && $this->Upload->delete()) {
			//print_r();
			$this->Session->setFlash(__('Preview deleted'));
			$file = new File(APP.'uploads'.DS.$params[1],false,0777);
			if($file->delete()) {
   				echo 'image deleted.....';
			}
			$this->redirect(array('action' => 'index'));
			print_r("nada");
		}
		$this->Session->setFlash(__('Preview was not deleted'));
		$this->redirect(array('action' => 'index'));
	}

	function uploadFile() {
  		$file = $this->data['Upload']['file'];
  		if ($file['error'] === UPLOAD_ERR_OK) {
    			$id = String::uuid();
    			if (move_uploaded_file($file['tmp_name'], APP.'uploads'.DS.$id)) {
      				//$data1 = array("Upload" => array("id" => "0","user_id" => "0","filename" => "0","filesize" => "0","filemime" => "0"));
      				$this->request->data['Upload']['id'] = $id;
      				$this->request->data['Upload']['user_id'] = $this->Auth->user('id');
      				$this->request->data['Upload']['filename'] = $file['name'];
      				$this->request->data['Upload']['filesize'] = $file['size'];
      				$this->request->data['Upload']['filemime'] = $file['type'];
				$this->request->data['Preview']['upload_id']=$id;
      				return true;
    			}
  		}
  		return false;
	}
	
	function editUploadFile($id) {
  		$file = $this->request->data['Upload']['file'];
  		if ($file['error'] === UPLOAD_ERR_OK) {
    			//$id = String::uuid();
    			if (move_uploaded_file($file['tmp_name'], APP.'uploads'.DS.$id)) {
      				$this->request->data['Upload']['id'] = $id;
      				$this->request->data['Upload']['user_id'] = $this->Auth->user('id');
      				$this->request->data['Upload']['filename'] = $file['name'];
      				$this->request->data['Upload']['filesize'] = $file['size'];
      				$this->request->data['Upload']['filemime'] = $file['type'];
      				return true;
    			}
  		}
  		return false;
	}
	function redirect1()
	{
		$this->redirect('/users/index');
	}

}
